﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sirenix.OdinInspector;
using System.Linq;
using System.Reflection;

[CreateAssetMenu(fileName = "BuffConfigItem", menuName = "Buff/BuffConfigItem")]
public class BuffConfigObject
#if !NOT_UNITY
    : SerializedScriptableObject
#endif
{
    [LabelText("Buff ID"),DelayedProperty]
    public int buffID;
    [LabelText("Buff名字"),DelayedProperty]
    public string buffName;
    [LabelText("Buff类型")]
    public BuffType buffType;
    [HideInInspector]
    public uint Duration;
    [LabelText("是否在状态栏显示"),UnityEngine.Serialization.FormerlySerializedAs("showInStatusBar")]
    public bool showInStatusBar;
    [LabelText("是否叠加")]
    public bool isStackable;
    [LabelText("最大叠加次数"),ShowIf("isStackable"),Range(1,100)]
    public int maxStackCount;
    [LabelText("是否可驱散")]
    public bool isDispellable;
    [LabelText("子Buff")]
    public bool hasChildBuff;
    [OnInspectorGUI("DrawSpace",append:true)]
    [HideReferenceObjectPicker]
    [LabelText("子Buff列表"),ShowIf("hasChildBuff"),ListDrawerSettings(DraggableItems = false, ShowItemCount = false, CustomAddFunction = "AddChildBuff")]
    public List<ChildBuff> childBuffList = new List<ChildBuff>();

    void AddChildBuff()
    {
        childBuffList.Add(new ChildBuff());
    }

    [LabelText("Buff列表"),Space(30)]
    [ListDrawerSettings(Expanded = true, DraggableItems = false, ShowItemCount = false, HideAddButton = true)]
    [HideReferenceObjectPicker]
    public List<BuffBase> Buffs = new List<BuffBase>();

    [HorizontalGroup(PaddingLeft = 40, PaddingRight = 40)]
    [HideLabel,OnValueChanged("AddBuff"),ValueDropdown("BuffTypeSelect")]
    public string EffectTypeName = "（添加Buff）";

    public IEnumerable<string> BuffTypeSelect()
    {
        var types = typeof(BuffBase).Assembly.GetTypes()
                    .Where(t => !t.IsAbstract)
                    .Where(t => typeof(BuffBase).IsAssignableFrom(t))
                    .Where(t => t.GetCustomAttribute<BuffAttribute>() != null)
                    .OrderBy(t => t.GetCustomAttribute<BuffAttribute>().Order)
                    .Select(t => t.GetCustomAttribute<BuffAttribute>().BuffType);
        var result = types.ToList();
        result.Insert(0, "（添加Buff）");
        return result;

    }

    void AddBuff()
    {
        if(EffectTypeName != "（添加Buff）")
        {
            var buffType = typeof(BuffBase).Assembly.GetTypes()
                    .Where(t => !t.IsAbstract)
                    .Where(t => typeof(BuffBase).IsAssignableFrom(t))
                    .Where(t => t.GetCustomAttribute<BuffAttribute>() != null)
                    .Where(t => t.GetCustomAttribute<BuffAttribute>().BuffType == EffectTypeName)
                    .FirstOrDefault();
            var buff = System.Activator.CreateInstance(buffType) as BuffBase;
            buff.Enabled = true;
            Buffs.Add(buff);
            EffectTypeName = "（添加Buff）";
        }
    }

    [LabelText("Buff特效")]
    [OnInspectorGUI("BeginBox", append: false)]
    public GameObject ParticleEffect;
    public GameObject GetParticleEffect() => ParticleEffect;

    [LabelText("Buff音效")]
    [OnInspectorGUI("EndBox", append: true)]
    public AudioClip Sound;
    [LabelText("Buff描述")]
    public string BuffDescription;

#if UNITY_EDITOR
    private void DrawSpace()
    {
        GUILayout.Space(20);
    }
    private void BeginBox()
    {
        GUILayout.Space(30);
        Sirenix.Utilities.Editor.SirenixEditorGUI.DrawThickHorizontalSeparator();
        GUILayout.Space(10);
        Sirenix.Utilities.Editor.SirenixEditorGUI.BeginBox("状态表现");
    }

    private void EndBox()
    {
        Sirenix.Utilities.Editor.SirenixEditorGUI.EndBox();
        GUILayout.Space(30);
        Sirenix.Utilities.Editor.SirenixEditorGUI.DrawThickHorizontalSeparator();
        GUILayout.Space(10);
    }
#endif 

}

public class ChildBuff
{
    [LabelText("Buff配置")]
    public BuffConfigObject buffConfig;

    [LabelText("参数列表"),HideReferenceObjectPicker]
    public Dictionary<string,string> Params = new Dictionary<string, string>();
}

public enum BuffType
{
    [LabelText("Buff(增益)")]
    Buff,
    [LabelText("Debuff(减益)")]
    Debuff,
    [LabelText("其他")]
    Other
}

public enum BuffTriggerType
{
    [LabelText("（空）")]
    None = 0,
    [LabelText("立即触发")]
    Instant = 1,
    [LabelText("条件触发")]
    Condition = 2,
    [LabelText("行动点触发")]
    Action = 3,
    [LabelText("间隔触发")]
    Interval = 4,
    [LabelText("在行动点且满足条件")]
    ActionCondition = 5,
}
